package com.thx.common.util;

/**
 * 字符串工具类.
 *
 * @author balance
 */
public class StringUtil {

  /**
   * 将字符数字转换为int类型，若转换异常，则返回默认值 default.
   */
  public static int string2Int(String valStr, int defaultStr) {
    try {
      return Integer.parseInt(valStr);
    } catch (Exception ex) {
      return defaultStr;
    }
  }

  /**
   * 首字母变小写.
   */
  public static String firstCharToLowerCase(String str) {
    char firstChar = str.charAt(0);
    if (firstChar >= 'A' && firstChar <= 'Z') {
      char[] arr = str.toCharArray();
      arr[0] += ('a' - 'A');
      return new String(arr);
    }
    return str;
  }

  /**
   * 首字母变大写.
   */
  public static String firstCharToUpperCase(String str) {
    char firstChar = str.charAt(0);
    if (firstChar >= 'a' && firstChar <= 'z') {
      char[] arr = str.toCharArray();
      arr[0] -= ('a' - 'A');
      return new String(arr);
    }
    return str;
  }

  /**
   * 字符串为 null 或者为 "" 时返回 true.
   */
  public static boolean isBlank(String str) {
    return str == null || "".equals(str.trim());
  }

  /**
   * 字符串不为 null 而且不为 "" 时返回 true.
   */
  public static boolean notBlank(String str) {
    return str != null && !"".equals(str.trim());
  }

  /**
   * 字符串们都不为 null 而且不为 "" 时返回 true.
   */
  public static boolean notBlank(String... strings) {
    if (strings == null) {
      return false;
    }
    for (String str : strings) {
      if (str == null || "".equals(str.trim())) {
        return false;
      }
    }
    return true;
  }

  /**
   * 字符串不为 null 时返回 true.
   */
  public static boolean notNull(Object... paras) {
    if (paras == null) {
      return false;
    }
    for (Object obj : paras) {
      if (obj == null) {
        return false;
      }
    }
    return true;
  }

  /**
   * 转化下划线形式字符串到驼峰式.
   *
   * @param stringWithUnderline 里面含有下划线的字符串
   * @return 转换后的字符串
   */
  public static String toCamelCase(String stringWithUnderline) {
    if (stringWithUnderline.indexOf('_') == -1) {
      return stringWithUnderline;
    }

    stringWithUnderline = stringWithUnderline.toLowerCase();
    char[] fromArray = stringWithUnderline.toCharArray();
    char[] toArray = new char[fromArray.length];
    int toIndex = 0;
    for (int i = 0; i < fromArray.length; i++) {
      if (fromArray[i] == '_') {
        // 当前字符为下划线时，将指针后移一位，将紧随下划线后面一个字符转成大写并存放
        i++;
        if (i < fromArray.length) {
          toArray[toIndex++] = Character.toUpperCase(fromArray[i]);
        }
      } else {
        toArray[toIndex++] = fromArray[i];
      }
    }
    return new String(toArray, 0, toIndex);
  }

  /**
   * 连接数组里面的字符串.
   *
   * @param stringArray 字符串数组
   * @return 返回连接后的字符串
   */
  public static String join(String[] stringArray) {
    StringBuilder sb = new StringBuilder();
    for (String s : stringArray) {
      sb.append(s);
    }
    return sb.toString();
  }

  /**
   * 用指定的连接符连接数据里面的字符串.
   *
   * @param stringArray 字符串数组
   * @param separator 分隔符
   * @return 返回连接后的字符串
   */
  public static String join(String[] stringArray, String separator) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < stringArray.length; i++) {
      if (i > 0) {
        sb.append(separator);
      }
      sb.append(stringArray[i]);
    }
    return sb.toString();
  }

}
